﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class PropertySignature : ModifiedSignatureWithParameters
{
    internal PropertySignature(SyntaxToken declareKeywordToken, Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.PropertySignature, declareKeywordToken, name, parent)
    {
    }


    protected override string GetFullName()
    {
        Debug.Assert(Parent != null);
        return $"{((ObjectTypeSignature)Parent).FullName}.{Name}";
    }


    protected override bool VerifyModifierToken(SyntaxToken token)
    {
        if (token.IsNode(SyntaxKinds.NullableKeywordToken))
        {
            if (IsNullable)
            {
                return false;
            }

            IsNullable = true;
            return true;
        }

        if (token.IsNode(SyntaxKinds.ReadOnlyKeywordToken))
        {
            if (IsReadOnly)
            {
                return false;
            }

            IsReadOnly = true;
            return true;
        }

        if (token.IsNode(SyntaxKinds.WriteOnlyKeywordToken))
        {
            if (IsWriteOnly)
            {
                return false;
            }

            IsWriteOnly = true;
            return true;
        }

        if (token.IsNode(SyntaxKinds.DefaultKeywordToken))
        {
            if (IsDefault)
            {
                return false;
            }

            IsDefault = true;
            return true;
        }

        return false;
    }
}


public static partial class NodeFactory
{
    public static PropertySignature CreatePropertySignature(SyntaxNode? parent, IEnumerable<SyntaxToken>? modifiers,
        SyntaxToken declareKeywordToken, Identifier identifierName, IEnumerable<ParameterSignature>? parameters,
        TypeBinding? binding)
    {
        Debug.Assert(declareKeywordToken.IsNode(SyntaxKinds.PropertyKeywordToken));
        PropertySignature signature = new(declareKeywordToken, identifierName, parent);

        if (modifiers != null)
        {
            foreach (SyntaxToken modifier in modifiers)
            {
                signature.TryAddModifier(modifier);
            }
        }

        if (parameters != null)
        {
            foreach (ParameterSignature param in parameters)
            {
                signature.AddParameter(param);
            }
        }

        if (binding != null)
        {
            signature.Update(binding);
        }

        return signature;
    }
}